using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace OrangeProject.ReactiveList.RabbitMQTransportCore
{
    class MyContractResolver : DefaultContractResolver
    {
        protected override List<MemberInfo> GetSerializableMembers(Type objectType)
        {
            var fields = objectType.GetTypeInfo().DeclaredProperties.ToList();
            var keepingField = fields
                .Where(field => (field.PropertyType == typeof(String) || field.PropertyType == typeof(String[])) && field.Name.StartsWith("__")).ToArray();
            foreach (var info in keepingField)
            {
                var field = fields.SingleOrDefault(f => f.Name == info.Name.Remove(0, 2));
                if (field != null)
                {
                    fields.Remove(field);
                }
            }

            var members = new List<MemberInfo>();
            members.AddRange(fields);
            return members;
        }

        protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            return base.CreateProperties(type, MemberSerialization.Fields);
        }
    }
}